package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import model.FundDAO;
import model.Model;
import databeans.Employee;
import databeans.Fund;

public class EmployeeViewFundsAction extends Action{
    private FundDAO fundDAO;

    public EmployeeViewFundsAction(Model model) {
        model.getEmployeeDAO();
        fundDAO = model.getFundDAO();
    }
    
    public String getName() {
        return "employeeViewFunds.do";
    }
    

    public String perform(HttpServletRequest request) {
        //ATTENTION
        // define target page
        //String loginPage = "employee/login-form.jsp";
    	String loginPage = "employee/employee-login.jsp";
        String originPage = "employee/view-funds.jsp";
        String successPage = originPage; 
        
        //set the employee
        Employee employee = null;

        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        request.setAttribute("user-type", "employee");
        request.setAttribute("page", "view-funds");
        
        //get all the parameter from the form
        /*
        String requestType = request.getParameter("Submit");
        String currentPassword = request.getParameter("txtCurrentPassword");
        String newPassword = request.getParameter("txtNewPassword");
        String confirmPassword = request.getParameter("txtConfirmPassword");
        */
        
        //reload the form field
        //No need to reload - DUH!
        //request.setAttribute("txtCurrentPassword", currentPassword);
        //request.setAttribute("txtNewPassword", newPassword);
        //request.setAttribute("txtConfirmPassword", confirmPassword);
         
        //check if employee has been logged in
        //request.getSession() will return the current session and if one does not exist, a new session will be cretaed.
        //request.getSession(true) will return the current session if one exists, if one doesn't exits a new one will be created.
        //So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false), it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.
        employee = (Employee) request.getSession(true).getAttribute("employee");
        if (employee == null) {
            errors.add("You need to log in");
            return loginPage ;
        }

        //validate if user has necessary qualification
        //check the session

        // check for get or post request
        //if there is no parameter get passed, return to origin page
        /*
        if (requestType == null) {
            messages.add("To change password, fill the below form");
            return originPage;     
        }
        */
        
        //validate the input
        /*
        if (currentPassword.trim().equals("")) {
            messages.add("Enter your current password");
            errors.add("Current password field is still empty");
            return originPage;
        }
        */

        
        // at this stage, all validation should have been done
        // for this case, no validation is required
    
        try {
            
            //change the password
            
            Fund[] fundsList = fundDAO.getFunds();
            
            if (fundsList == null) {
                messages.add("There is no fund currently available to be seen");
                return successPage;
            }

            // Attach (this copy of) the employee bean to the session
            HttpSession session = request.getSession(true); 
            session.setAttribute("employee", employee);
            request.setAttribute("fundsList", fundsList);
            
            //set the welcome message
            //messages.add("Hi " + employee.getFirstName() + "!, your password has been succesfully changed");
            return successPage;

        } catch (Exception e) {
            errors.add("Unable to get list of funds.");
            return originPage;
        }

    }
}